package core;

public class Matrix {

	// mValues[linha][coluna]
	private double mValues[][];
	
	private int mNumColumns = 0;
	
	private int mNumRows = 0;
	
	public Matrix(int i, int j) {
		mValues = new double[i][j];
		mNumColumns = j;
		mNumRows = i;
	}
	
	public Matrix(double[][] values) {
		mNumRows = values.length;
		mNumColumns = values[0].length;
		mValues = values;
	}
	
	public void identidade() {
		
		int size = Math.min(mNumRows, mNumColumns);
		
		for(int i = 0; i < size; i++) {
			mValues[i][i] = 1;
		}
	}
	
	public void set(int i, int j, double value) {
		mValues[i][j] = value;
	}
	
	public double get(int i, int j) {
		return mValues[i][j];
	}

	public int getNumColumns() {
		return mNumColumns;
	}
	
	public int getNumRows() {
		return mNumRows;
	}
	
	public static Matrix mult(Matrix m1, Matrix m2) {
		
		Matrix m = new Matrix(m1.getNumRows(), m2.getNumColumns());
		
		for(int i = 0; i < m1.getNumRows(); i++) {
			for(int j = 0; j < m2.getNumColumns(); j++) {
				for(int k = 0; k < m2.getNumColumns(); k++)
				m.mValues[i][j] += m1.mValues[i][k] * m2.mValues[k][j];
			}
		}
		
		return m;
	}
	
	public static Point3D mult(Point3D p, Matrix m2) {
		
		Matrix m = new Matrix(1, 3);
		
		int value = 0;
		
		for(int j = 0; j < m2.getNumColumns(); j++) {
			for(int k = 0; k < m2.getNumColumns(); k++){
				switch(k) {
				case 0:
					value = p.getX();
					break;
				case 1:
					value = p.getY();
					break;
				case 2:
					value = p.getZ();
				}
				
				m.mValues[0][j] += value * m2.mValues[k][j];
			}
		}
		
		p.setX((int)m.mValues[0][0]);
		p.setY((int)m.mValues[0][1]);
		p.setZ(1);
		
		return p;
	}
	
	public static Matrix point2Matrix(Point3D p3) {
		double[][] m = {{p3.getX(), p3.getY(), p3.getZ()}};
		
		return new Matrix(m);
	}
	
	public static Point3D matrix2Point(Matrix m) {
		Point3D p = new Point3D((int)m.mValues[0][0], (int)m.mValues[0][1], (int)m.mValues[0][2]);
		return p;
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		
		for(int i = 0; i < mNumRows; i++) {
			for(int j = 0; j < mNumColumns; j++) {
				builder.append(mValues[i][j]).append(",");
			}
			builder.append("\n");
		}
		
		return builder.toString();
	}
	
	public static void main(String args[]) {
		double[][] d1 = {{2, 3, 5}, {2, 1, 4}, {0, 3, 6}};
		double[][] d2 = {{2, 4, 1}, {7,3,5}, {0, 2, 1}};
 		Matrix m1 = new Matrix(d1);
		Matrix m2 = new Matrix(d2);
		
		Matrix m = Matrix.mult(m1, m2);
		
		System.out.println("Matriz \n" + m.toString());
	}
}
